"""
数据库路由规则
"""


class DjangoDbRouter:
    """数据库分库的路由"""

    def db_for_read(self, model, **hints):
        if model._meta.app_label in ('auth', 'admin', 'django', 'contenttypes'):
            return 'django'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in ('auth', 'admin', 'django', 'contenttypes'):
            return 'django'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return True


class MasterSlaveDbRouter:
    """主从复制的路由"""

    def db_for_read(self, model, **hints):
        # return random.choice(('slave1', 'slave2'))
        return 'slave1'

    def db_for_write(self, model, **hints):
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return True
